<?php
namespace app\business;

class Product {

    protected $model = null;

    public function __construct() {
        $this->model = new \app\model\mysql\Product();
    }

    public function pageList($page = 1, $data = []) : array {
        $where = [];
        $order = [
            "list_order" => "ASC",
            "id" => "DESC",
        ];
        $pageSize = 2;
        $fields = "";

        if (isset($data["where"])){
            $where = array_merge($where, $data["where"]);
        }
        if (isset($data["order"])){
            $order = array_merge($order, $data["order"]);
        }
        if (isset($data["pageSize"])) {
            $pageSize = $data["pageSize"];
        }
        if (isset($data["fields"])) {
            $fields = $data["fields"];
        }

        $paginate = [
            "list_rows" => $pageSize,
            "page" => $page
        ];
        try{
            $result = $this->model->pageList($paginate, $where, $order, $fields);
            if ($result && $result->count() > 0) {
                return $result->toArray();
            }

            return [];
        }catch (Exception $e){
            Log::error($e->getMessage());
            return [];
        }
    }

}

